package com.exe.project.data;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.exe.project.interfaces.IReadXml;


public class ReadXml implements IReadXml {
	private File filetoConv = null;
	private File fileConv = null;
	private String directory ="";
	private Source source=null;
	private Result result =null;
	private Transformer transformer =null;
	private DocumentBuilderFactory docBuilderFactory = null;
	private DocumentBuilder docBuilder = null;
	private Document doc = null;
	
	public ReadXml(){}
	
	private void printChild(Node nodo,int level ){
		NodeList listnodes = nodo.getChildNodes();
		for(int j=0;j<listnodes.getLength();j++){
			if(listnodes.item(j).getNodeType() == Node.ELEMENT_NODE){
				if(level>2){
					nodo.removeChild(listnodes.item(j));
					j--;
				}
				if(listnodes.item(j).hasChildNodes()){
					printChild(listnodes.item(j),(level+1));
				}
			}
		}
	}
	
	/**
	 * This method create yhe new xml into disk, using the directory by default or a directory insert in the web.xml
	 * @param doc The document with the information about the xml
	 */
	private void generateXml(Document doc) {
		try{
			source = new DOMSource(doc);
			if(directory.length()>0){
				fileConv = new File(directory+"resultado.xml");
				result = new StreamResult(fileConv);
			}else{
				result = new StreamResult(new java.io.File("resultado.xml"));
			}
			transformer = TransformerFactory.newInstance().newTransformer();
			transformer.transform(source, result);
			/*Result console= new StreamResult(System.out);
			transformer.transform(source, console);*/
		}catch(Exception e){
			System.err.println("Error: "+e);
		}
	}
	
	public void convertXML(){
		try {
			docBuilderFactory = DocumentBuilderFactory.newInstance();
			docBuilder = docBuilderFactory.newDocumentBuilder();
			doc = docBuilder.parse (filetoConv);
			NodeList listnodes = doc.getChildNodes();
			printChild(listnodes.item(0),1);
			generateXml(doc);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	public File getFiletoConv() {
		return filetoConv;
	}

	public void setFiletoConv(File filetoConv) {
		this.filetoConv = filetoConv;
	}
	
	public void setDirectory(String path){
		File tmpFile = new File(path);
		if(!tmpFile.isDirectory()){
			if(!tmpFile.exists()){
				tmpFile.mkdir();
			}
		}
		this.directory = path;
	}

	public String getPathArchive(){
		return fileConv.toURI().toString();
	}
}
